package com.ruoyi.web.controller.help;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.help.domain.Appointment;
import com.ruoyi.help.service.IAppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 帮扶预约Controller
 *
 * @author ruoyi
 * @date 2023-05-19
 */
@RestController
@RequestMapping("/system/appointment")
public class AppointmentController extends BaseController {
    @Autowired
    private IAppointmentService appointmentService;

    /**
     * 查询帮扶预约列表
     */
    @PreAuthorize("@ss.hasPermi('system:appointment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Appointment appointment) {
        startPage();
        List<Appointment> list = appointmentService.selectAppointmentList(appointment);
        return getDataTable(list);
    }

    /**
     * 导出帮扶预约列表
     */
    @PreAuthorize("@ss.hasPermi('system:appointment:export')")
    @Log(title = "帮扶预约", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Appointment appointment) {
        List<Appointment> list = appointmentService.selectAppointmentList(appointment);
        ExcelUtil<Appointment> util = new ExcelUtil<>(Appointment.class);
        util.exportExcel(response, list, "帮扶预约数据");
    }

    /**
     * 获取帮扶预约详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:appointment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(appointmentService.selectAppointmentById(id));
    }

    /**
     * 新增帮扶预约
     */
    @PreAuthorize("@ss.hasPermi('system:appointment:add')")
    @Log(title = "帮扶预约", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Appointment appointment) {
        int i = appointmentService.insertAppointment(appointment);
        if (i == -5) {
            return AjaxResult.error("不是本班级学员");
        }
        return toAjax(appointmentService.insertAppointment(appointment));
    }

    /**
     * 修改帮扶预约
     */
    @PreAuthorize("@ss.hasPermi('system:appointment:edit')")
    @Log(title = "帮扶预约", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Appointment appointment) {
        return toAjax(appointmentService.updateAppointment(appointment));
    }

    /**
     * 删除帮扶预约
     */
    @PreAuthorize("@ss.hasPermi('system:appointment:remove')")
    @Log(title = "帮扶预约", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(appointmentService.deleteAppointmentByIds(ids));
    }
}
